package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.document.management.RegisterDocumentSet;
import cn.org.chiss.rhin._2015.document.management.RegisterDocumentSetResponse;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 健康档案管理服务-注册健康档案索引
 */
@Service("AuditInfoConverter4TerminologyDocumentManagerRegister")
public class AuditInfoConverter4TerminologyDocumentManagerRegister extends BaseAuditInfoConverter<RegisterDocumentSet, RegisterDocumentSetResponse> {

    public AuditInfoConverter4TerminologyDocumentManagerRegister() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.DOCUMENTS_MANAGEMENT,
                EventTypeCodeEnum.REGISTER_DOCUMENT,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.MASTER_FILE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-management-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_MANAGEMENT.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(RegisterDocumentSet documentStoredQuery, RegisterDocumentSetResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(RegisterDocumentSet documentStoredQuery, RegisterDocumentSetResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(RegisterDocumentSet documentStoredQuery, RegisterDocumentSetResponse documentStoredQueryResponse) {
        return new AuditParticipantUserDesc("documents-management-service", ServiceCodeEnum.DOCUMENTS_MANAGEMENT.getName());
    }

    @Override
    String getObjectId(RegisterDocumentSet documentStoredQuery, RegisterDocumentSetResponse documentStoredQueryResponse) {
        if (Objects.nonNull(documentStoredQuery)
                && Objects.nonNull(documentStoredQuery.getSubmissionSet())
                && Objects.nonNull(documentStoredQuery.getSubmissionSet().getId())) {
            return documentStoredQuery.getSubmissionSet().getId().getValue();
        }
        if (Objects.nonNull(documentStoredQueryResponse)
                && null != documentStoredQueryResponse.getSubmissionSetUUID()) {
            return documentStoredQueryResponse.getSubmissionSetUUID().getValue();
        }
        return "";
    }

    @Override
    public boolean isMatch(String method, Object data) {
        return "DocumentManagementPortImpl.documentStoredQuery".equals(method);
    }
}
